package com.resumes.resumesdataservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resumes.resumesdataservice.domain.*;
import com.resumes.resumesdataservice.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Transactional(rollbackFor=Exception.class)//事务回滚
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private ProjectExperienceService projectExperienceService;
    @Resource
    private AwardInformationService awardInformationService;
    @Resource
    private WxssService wxssService;
    @Resource
    private StudentPhotoService studentPhotoService;
    @Resource
    private FileService fileService;
    @Resource
    private VideoService videoService;
    public String RandomColor(){
        List<String> colors = Arrays.asList(
                "118deg, #34776d 0%, #c3fa8e 48%, #d7a8f6 17%, #3f89a3 100%",
                "48deg, #f2bef4 0%, #4d1aa4 48%, #596172 17%, #dfe3e7 120%",
                "118deg,#15467f 7%,#e5e4f1 67%,#932404 30%, #dfa197 100%",
                "259deg,#567b43 9%,#f7e627 67%,#1a4b6b 5%, #176f62 100%");
        return colors.get(new Random().nextInt(colors.size()));
    }


    @RequestMapping(value = "searchByArgs")
    public List<ListStudent> searchByArgs(String args){
        return studentService.searchByArgs(args);
    }

    @RequestMapping("/getList")
    public List<ListStudent> getList() {
        List<ListStudent> list = studentService.new_list();
        return list;
    }

    @RequestMapping("/login")
    public JSON login(String id, String pwd) {
        JSONObject json = new JSONObject();
        Student YNlo = studentService.login(id, pwd);//{{id,pwd},{id,pwd}}
        System.out.println(YNlo);
        if (YNlo.getLoginid() != null && YNlo.getLoginpwd() != null) {
            json.put("code",200);
            json.put("msg","登录成功");
            json.put("loginid",YNlo.getLoginid());
            json.put("loginpwd",YNlo.getLoginpwd());
            return json;
        } else {
            json.put("code",400);
            json.put("msg","登录失败");
            return json;
        }
    }

    @RequestMapping("/save")
    public synchronized boolean save(AllArgs args,HttpServletRequest request) {

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //将args中的数据分别转换为Student和ProjectExperience和AwardInformation
        Student student = args.getStudent();

        String loginid = request.getHeader("loginid");
        String loginpwd = request.getHeader("loginpwd");

        student.setLoginid(loginid);
        student.setLoginpwd(loginpwd);
        student.setId(uuid);

        ProjectExperience projectExperience = args.getProjectExperience();
        projectExperience.setStudentId(uuid);
        AwardInformation awardInformation = args.getAwardInformation();
        awardInformation.setStudentId(uuid);

        studentService.save(student);
        projectExperienceService.save(projectExperience);
        awardInformationService.save(awardInformation);

        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer1 = new StringBuffer();
        StringBuffer stringBuffer2 = new StringBuffer();


        ExecutorService executorService = Executors.newFixedThreadPool(3);

        try {
            executorService.execute(()->{
                for (String s : args.getPhotopath()) {
                    stringBuffer.append(s);
                }
                //将stringBuffer中的中括号和双引号全部删除
                String photoPath = stringBuffer.toString().replaceAll("[\\[\\]\"]", "");
                studentPhotoService.save(new StudentPhoto(UUID.randomUUID().toString(),uuid, photoPath));
                wxssService.save(new Wxss(RandomColor(), photoPath, uuid));//首页头像存入wxss表
            });
            executorService.execute(()->{
                for (String s : args.getVideopath()) {
                    stringBuffer1.append(s);
                }
                //将stringBuffer中的中括号和双引号全部删除
                String videoPath = stringBuffer1.toString().replaceAll("[\\[\\]\"]", "");
                videoService.save(new Video(UUID.randomUUID().toString(),uuid, videoPath));
            });
            executorService.execute(()->{
                for (String s : args.getFilepath()) {
                    stringBuffer2.append(s);
                }
                //将stringBuffer中的中括号和双引号全部删除
                String filePath = stringBuffer2.toString().replaceAll("[\\[\\]\"]", "");
                fileService.save(new File(UUID.randomUUID().toString(),uuid, filePath));
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            executorService.shutdown();//关闭线程池
        }

        log.info("save student success");
        return true;
    }

    @RequestMapping("/getInfo")
    public TextInfo getInfo(String id) {
        TextInfo textInfo = studentService.getInfo(id);
        return textInfo;
    }
}
